package com.css.fxfzfxqh.modules.lifelineEngineering.controller;

import com.css.fxfzfxqh.base.response.RestResponse;
import com.css.fxfzfxqh.common.rest.model.param.AnalysisParam;
import com.css.fxfzfxqh.common.rest.model.vo.ysx.EvaluationResultsVo;
import com.css.fxfzfxqh.modules.lifelineEngineering.entity.EvaluationResults;
import com.css.fxfzfxqh.modules.lifelineEngineering.param.DamageLevelAnalysisParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.service.EstimateService;
import com.css.fxfzfxqh.modules.lifelineEngineering.vo.FailureAnalysisParam;
import com.css.fxfzfxqh.modules.lifelineEngineering.vo.VectorVo;
import com.css.fxfzfxqh.modules.util.hypergraph.service.AsyncService;
import com.css.fxfzfxqh.modules.util.hypergraph.service.HypergraphService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 评估
 */
@RestController
@RequestMapping("estimate")
@Slf4j
public class EstimateController {

    @Autowired
    private EstimateService estimateService;
    @Resource
    AsyncService asyncService;

    /**
     * 查询当前任务
     */
    @GetMapping("getCurrentTask")
    public RestResponse getCurrentTask(){
        try {
            return RestResponse.succeed(estimateService.getCurrentTask());
        } catch (Exception e) {
            log.error("get page error:{}",e.getMessage(),e);
            return RestResponse.fail("查询失败!");
        }
    }

    /**
     * 导出excel
     * @param response
     */
    @GetMapping("exportExcel")
    public void exportExcel(String taskId,Integer type,HttpServletResponse response){
        try {
            estimateService.exportExcel(taskId,type,response);
        } catch (Exception e) {
            log.error("get exportExcel error:{}",e.getMessage(),e);
        }
    }

    /**
     * 导出shp
     */
    @GetMapping("exportShp")
    public void exportShp(String taskId,Integer type,HttpServletResponse response){
        try {
            estimateService.exportShp(taskId,type,response);
        } catch (Exception e) {
            log.error("get exportShp error:{}",e.getMessage(),e);
        }
    }

    /**
     * 破坏等级分析
     */
    @PostMapping("damageLevelAnalysis")
    public RestResponse damageLevelAnalysis(@RequestBody List<AnalysisParam> listParam){
        try {
            HashMap<Integer, Object> map = estimateService.damageLevelAnalysis(listParam);
            //超图刷新图层
            asyncService.asyncRefresh("fxqh_smx_evaluation_results");
            //超图刷新图层
            asyncService.asyncRefresh("fxqh_smx_evaluation_results_line");
            return RestResponse.succeed(map);
        } catch (Exception e) {
            log.error("get damageLevelAnalysis error:{}",e.getMessage(),e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 根据编码查矢量数据
     */
    @GetMapping("getVectorByVersionId")
    public RestResponse getVectorByVersionId(String versionId,Integer type){
        try {
            VectorVo entity = estimateService.getVectorByVersionId(versionId,type);
            return RestResponse.succeed(entity);
        } catch (Exception e) {
            log.error("get getVectorByVersionId error:{}",e.getMessage(),e);
            return RestResponse.fail("失败!");
        }
    }

    /**
     * 导出excel
     * @param response
     */
    @GetMapping("exportAllExcel")
    public void exportAllExcel(String taskId,HttpServletResponse response){
        try {
            estimateService.exportAllExcel(taskId,response);
        } catch (Exception e) {
            log.error("get exportAllExcel error:{}",e.getMessage(),e);
        }
    }
}
